<template>
    <div class="register">
        <section class="form_container">
          
            <el-form :model="user" :rules="rules" class="userForm" ref="userForm" label-width="80px" >
                <el-form-item label="用户名" prop="name">
                    <el-input v-model="user.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="user.password" placeholder="密码" type="password"></el-input>
                </el-form-item>                
                <el-form-item>
                    <el-button type="primary"  class="submit_btn" @click="submitForm('registerForm')">修改</el-button>
                </el-form-item>
            </el-form>
        </section>
    </div>
</template>

<script>
import http from "../../utils/https";
export default {
  name: "register",
 //    判断密码是否一致；
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.registerUser.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      user: {
        name: "",
        email: "",
        password: "",
        username: "",
      },
//    在return 后面；
      rules: {
      // 要以数组形式展示
        name: [
          { required: true, message: "用户名不能为空", trigger: "change" },
          { min: 2, max: 30, message: "长度在 2 到 30 个字符", trigger: "blur" }
        ],
        email: [
          {
            type: "email",
            required: true,
            message: "邮箱格式不正确",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 30, message: "长度在 6 到 30 个字符", trigger: "blur" }
        ],
        password2: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 30,
            message: "长度在 6 到 30 个字符",
            trigger: "blur"
          },
          { validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .post("/api/users/register", this.registerUser)
            .then(res => {
              // 注册成功
              this.$message({
                message: "注册成功！",
                type: "success"
              });
              // this.$router.push("/login");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  },
  mounted() {
    http.get({
      url:'/yjl/user/id',
     
    }).then(res => {
      if(res.code = 200){
        this.user=res.content; 
      }else{
                this.$message.error('接口异常');
      }
    }).catch(err => {
      console.log(err)
    })
  },
};


  
</script>